package com.spring.boot.demo.controller;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/11/19 22:24
 * @Description ES 测试类
 */
@RestController
@RequestMapping("/api/es")
public class TestController {

    @Autowired
    private RestHighLevelClient highRestClient;

    @GetMapping("/createIndex")
    public String createIndex(String indexName) throws IOException {
        // 获取索引客户端
        IndicesClient indicesClient = highRestClient.indices();
        // 创建索引请求对象: 传入一个索引名称
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        // 执行创建索引的请求
        // RequestOptions.DEFAULT 指定名称的配置项
        CreateIndexResponse response = indicesClient.create(createIndexRequest, RequestOptions.DEFAULT);
        // 获取响应信息
        String index = response.index();
        System.out.println("索引名称: " + index);
        // 索引是否创建成功
        boolean acknowledged = response.isAcknowledged();
        System.out.println("索引是否创建成功: " + acknowledged);
        return "success.";
    }

    @GetMapping("/deleteIndex")
    public String deleteIndex() throws IOException {
        // 获取索引客户端
        IndicesClient indicesClient = highRestClient.indices();
        // 创建删除索引的请求
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("idx_name_2022");
        // 创建执行删除索引的请求
        // RequestOptions.DEFAULT 指定名称的配置项
        AcknowledgedResponse response = indicesClient.delete(deleteIndexRequest, RequestOptions.DEFAULT);
        // 索引是否创建成功
        boolean acknowledged = response.isAcknowledged();
        System.out.println("索引是否删除成功: " + acknowledged);
        return "success.";
    }

    @GetMapping("/getIndex")
    public String getIndex() throws IOException {
        // 获取索引客户端
        IndicesClient indicesClient = highRestClient.indices();
        // 创建获取索引的请求对象
        // 当索引不存在的时候，就会抛出一个异常: ElasticsearchStatusException
        GetIndexRequest getIndexRequest = new GetIndexRequest("idx_name_2021", "idx_name_2022");
        // 执行请求
        // RequestOptions.DEFAULT 指定名称的配置项
        GetIndexResponse response = indicesClient.get(getIndexRequest, RequestOptions.DEFAULT);
        // 处理响应信息
        String[] indices = response.getIndices();
        for (String index : indices) {
            System.out.println("索引名称是: " + index);
        }
        return "success.";
    }

    @GetMapping("/existsIndex")
    public String existsIndex() throws IOException {
        // 获取索引客户端
        IndicesClient indicesClient = highRestClient.indices();
        // 创建获取索引的请求对象
        // 当索引不存在的时候，就会抛出一个异常: ElasticsearchStatusException
        GetIndexRequest getIndexRequest = new GetIndexRequest("idx_name_2021", "idx_name_2022");
        // 执行请求
        // RequestOptions.DEFAULT 指定名称的配置项
        boolean exists = indicesClient.exists(getIndexRequest, RequestOptions.DEFAULT);
        if (exists) {
            // 索引都存在
            return "index exists.";
        }
        // 索引不存在
        return "index does not exists.";
    }

}
